/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ch.haclyon.driveimageview.driveimageview.slice;

import ch.haclyon.driveimageview.driveimageview.ResourceTable;
import ch.haclyon.driveimageview.driveimageview.fraction.DetailFraction;
import ch.haclyon.driveimageview.driveimageview.fraction.MainFraction;
import ch.haclyon.driveimageview.driveimageview.utils.JUtils;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.window.dialog.CommonDialog;


/**
 * MainAbilitySlice
 *
 * @author wangyin
 * @since 2021/06/23
 */
public class MainAbilitySlice extends AbilitySlice implements MainFraction.CallBack, Component.ClickedListener {
    /**
     * sampleTexts
     */
    public static String[] sampleTexts = new String[]
            {"check the map", "watching out", "on our way", "forest way", "warming up"};

    /**
     * samplePictures
     */
    public static int[] samplePictures = new int[]{ResourceTable.Media_dscf4046,
            ResourceTable.Media_dscf4131,
            ResourceTable.Media_dscf7236,
            ResourceTable.Media_dscf7252,
            ResourceTable.Media_dscf7346};

    /**
     * sampleColours
     */
    public static String[] sampleColours = new String[]{"#F44336", "#009688", "#4CAF50", "#FF5722", "#795548"};

    /**
     * sampleHeights
     */
    public static float[] sampleHeights = new float[]{50f, 100f, 60f, 45f, 125f};

    /**
     * mainFraction
     */
    private MainFraction mainFraction;

    /**
     * back
     */
    private Image back;

    /**
     * currentPosition
     */
    private int currentPosition = 0;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initComponents();
        back = (Image) findComponentById(ResourceTable.Id_back);
        findComponentById(ResourceTable.Id_dl_back).setClickedListener(this);
        findComponentById(ResourceTable.Id_help).setClickedListener(this);
    }

    /**
     * initComponents
     */
    private void initComponents() {
        mainFraction = new MainFraction();
        mainFraction.setCallBack(this);
        currentPosition = 0;
        showFraction(mainFraction);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void entryDetail(int position) {
        back.setVisibility(Component.VISIBLE);
        currentPosition = 1;
        showFraction(DetailFraction.getInstance(position));
    }

    /**
     * showFraction
     *
     * @param showFraction showFraction
     */
    private void showFraction(Fraction showFraction) {
        if (getAbility() instanceof FractionAbility) {
            FractionAbility ability = (FractionAbility) getAbility();
            ability.getFractionManager().startFractionScheduler().replace(ResourceTable.Id_main_fraction, showFraction).submit();
        }
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_dl_back:
                back.setVisibility(Component.INVISIBLE);
                currentPosition = 0;
                showFraction(mainFraction);
                break;
            case ResourceTable.Id_help:
                // 获取自定义的xml
                Component componentXml = LayoutScatter.getInstance(this)
                        .parse(ResourceTable.Layout_aboutdialog, null, true);
                CommonDialog commonDialog = new CommonDialog(this);
                commonDialog.setSize((int) JUtils.vp2px(this, 300F),
                        (int) JUtils.vp2px(this, 410F));
                // 绑定自定义的xml到commonDialog中
                commonDialog.setContentCustomComponent(componentXml);
                componentXml.findComponentById(ResourceTable.Id_cancel)
                        .setClickedListener(component1 -> commonDialog.hide());
                commonDialog.setDialogListener(() -> {
                    commonDialog.hide();
                    return false;
                });
                commonDialog.show();
                break;
        }
    }

    @Override
    protected void onBackPressed() {
        if (currentPosition == 0) {
            super.onBackPressed();
        } else {
            back.setVisibility(Component.INVISIBLE);
            currentPosition = 0;
            showFraction(mainFraction);
        }
    }
}
